﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class KeyColliderTrack : MonoBehaviour
{
    static bool key1 = false;
    static bool key2 = false;
    static bool key3 = false;

    private AudioSource audios;
    public AudioClip Adsorb;

    private void Start()
    {
        audios = GameObject.Find("Main Camera").GetComponent<AudioSource>();
    }

    private void OnTriggerEnter(Collider other)
    {

        if (other.gameObject.name == "Door_Lock01" && gameObject.name=="c1")
        {
            Debug.Log("钥匙一");
            other.gameObject.GetComponent<DragKey>().canMove = false;
            other.gameObject.GetComponent<Rigidbody>().isKinematic = true;
            other.gameObject.transform.localPosition = new Vector3(0, 0, 0);
            other.gameObject.transform.localEulerAngles = new Vector3(0, 0, 0);
            key1 = true;
            PlayClip();
            
        }
        else if (other.gameObject.name == "Door_Lock02" && gameObject.name == "c2")
        {
            Debug.Log("钥匙二");
            other.gameObject.GetComponent<DragKey>().canMove = false;
            other.gameObject.GetComponent<Rigidbody>().isKinematic = true;
            other.gameObject.transform.localPosition = new Vector3(0, 0, 0);
            other.gameObject.transform.localEulerAngles = new Vector3(0, 0, 0);
            key2 = true;
            PlayClip();
        }
        else if (other.gameObject.name == "Door_Lock03" && gameObject.name == "c3")
        {
            Debug.Log("钥匙三");
            other.gameObject.GetComponent<DragKey>().canMove = false;
            other.gameObject.GetComponent<Rigidbody>().isKinematic = true;
            other.gameObject.transform.localPosition = new Vector3(0, 0, 0);
            other.gameObject.transform.localEulerAngles = new Vector3(0, 0, 0);
            key3 = true;
            PlayClip();
        }

    }

   void PlayClip()
    {
        audios.clip = Adsorb;
        audios.Play();
        audios.loop = false;
    }

    IEnumerator Walk()
    {
        yield return new WaitForSeconds(1);
        
        PlayerControl.Instance.WalkToDoor();
    }

    private void Update()
    {
        if (key1 == true && key2 == true && key3 == true)
        {
            StartCoroutine("Walk");
            key1 = false;
            key2 = false;
            key3 = false;
        }
    }
}
